from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
import re



class ReginsterView(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')
        password1 = request.data.get('password1')

        if not([username,password,password1]):
            return Response({'msg':'数据不完整'})
        if re.match(r'^[a-z][A-Z]{5-20}$',username):
            return Response({'msg':'用户名不合法'})
        if re.match(r"^/d{8-20}$",password):
            return Response({'msg':'密码不合法'})
        if password!=password1:
            return Response({'msg':'确认密码错误'})
        else:
            return Response({'msg':'注册成功'})

class LoginView(APIView):
    def post(self,request):
        username = request.data.get('username')
        password = request.data.get('password')

        user = User.objects.filter(username=username,password=password).first()
        if user:
            return Response({'msg':'登录成功','code':200,'uid':user.id})
        else:
            return Response({'msg':'登录失败','code':400})

